/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-11-06
 * Time: 15:26
 */
class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class Day14 {

    //2.两数相加 - 错误
    public static ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        int preRet = 0;
        int ret = 0;
        ListNode preL1 = null;
        ListNode head = l1;
        while (l1 != null && l2 != null){
            ret = (l1.val + l2.val) % 10;
            int cnt = l1.val;
            l1.val = preRet + ret;
            preRet = (cnt + l2.val) / 10;
            preL1 = l1;
            l1 = l1.next;
            l2 = l2.next;
        }

        ListNode cur = preL1;
        if (l2 != null) {
            cur.next = l2;
            ListNode preCur = l2;
            while (l2 != null) {
                ret = (l2.val + preRet) % 10;
                int cnt = l2.val;
                l2.val = ret;
                preRet = cnt / 10;
                preCur = l2;
                l2 = l2.next;
            }
            if (preRet == 1) {
                ListNode node = new ListNode(1);
                preCur.next = node;
            }
        }
        if (l1 != null){
            ListNode preCur = l1;
            while (l1 != null) {
                ret = (l1.val + preRet) % 10;
                int cnt = l1.val;
                l1.val = ret;
                preRet = cnt / 10;
                preCur = l1;
                l1 = l1.next;
            }
            if (preRet == 1) {
                ListNode node = new ListNode(1);
                preCur.next = node;
            }
        }
        return head;
    }

    //两数之和 - 好好看 - 标记
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode cur = head;
        int ret = 0;

        while (l1 != null || l2 != null || ret != 0){
            int val1 = l1 == null ? 0 : l1.val;
            int val2 = l2 == null ? 0 : l2.val;
            int sumVal = val1 + val2 + ret;
            ret = sumVal / 10;

            ListNode sumNode = new ListNode(sumVal % 10);
            cur.next = sumNode;
            cur = sumNode;

            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        return head.next;
    }

    //19.删除链表的倒数第N个节点
    public ListNode removeNthFromEnd(ListNode head, int n) {
        return head;
    }
}
